package com.rock.code.bigdata.kpicontrast.service.impl;

import com.rock.code.bigdata.alarmanalysis.service.impl.alarmAnalysisServiceImpl;
import com.rock.code.bigdata.kpicontrast.dao.KpiContrastDao;
import com.rock.code.bigdata.kpicontrast.service.KpiContrastService;
import com.rock.code.bigdata.load.service.LoadService;
import com.rock.code.bigdata.load.service.impl.LoadServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by SJ on 2019/1/8.
 */

@Service
public class KpiContrastServiceImpl implements KpiContrastService {

    @Resource
    private KpiContrastDao kpiContrastDao;
    @Resource
    private LoadService loadServiceImpl;

    @Override
    public List queryResouceByName(String DI_QU, String CI_TYPE) {
        return kpiContrastDao.queryResouceByName(DI_QU, CI_TYPE);
    }

    @Override
    public HashMap queryResourceNumber(String kbpCodeStr, String areaFlag) {
        HashMap resultMap = new HashMap();
        try {
            List<String> kbpCodeList = Arrays.asList(kbpCodeStr.split(","));
            for (int i = 0; i < kbpCodeList.size(); i++) {
                String kbpCode = kbpCodeList.get(i);
                List list = kpiContrastDao.queryResourceNumber(kbpCode, areaFlag);
                Map resourceCount = kpiContrastDao.queryResourceCount(kbpCode, areaFlag);
                list.add(resourceCount);
                resultMap.put(kbpCode, list);//用kbpCode做key
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public HashMap queryUsability(String kpiCodeStr, String kbpCodeStr) {
        HashMap map = new HashMap();
        HashMap resultMap = new HashMap();
        try {
            String dateTime = alarmAnalysisServiceImpl.lastMonthBefore();
            String tableName = "tb_kpi_value_year_" + dateTime.split("-")[0];
           /* map.put("dateTime", dateTime);
            map.put("tableName", tableName);*/
            map.put("dateTime", "2018-01");
            map.put("tableName", "tb_kpi_value_year_2018");

            List<String> kbpCodeList = Arrays.asList(kbpCodeStr.split(","));
            List<String> kpiCodeList = Arrays.asList(kpiCodeStr.split(","));
            for (int i = 0; i < kbpCodeList.size(); i++) {
                String kbpCode = kbpCodeList.get(i);
                String kpiCode = kpiCodeList.get(i);
                map.put("kbpCode", kbpCode);
                map.put("kpiCode", kpiCode);
                List list = kpiContrastDao.queryUsability(map);
                resultMap.put(kbpCode, list);//用kbpCode做key
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public HashMap queryHealth(String kbpCodeStr, String areaFlag) {
        HashMap map = new HashMap();
        HashMap resultMap = new HashMap();
        try {
            String startDate = alarmAnalysisServiceImpl.getLastMonthStartDate();
            String endDate = alarmAnalysisServiceImpl.getLastMonthEndDate();
            if (!"".equals(startDate) && !"null".equals(startDate)) {
                /*map.put("startDate", startDate);*/
                map.put("startDate", "2018-01-01 00:00:00");
            }
            if (!"".equals(endDate) && !"null".equals(endDate)) {
                /*map.put("endDate", endDate);*/
                map.put("endDate", "2018-01-31 23:59:59");
            }

            List<String> kbpCodeList = Arrays.asList(kbpCodeStr.split(","));
            for (int i = 0; i < kbpCodeList.size(); i++) {
                String kbpCode = kbpCodeList.get(i);
                map.put("kbpCode", kbpCode);
                map.put("areaFlag", areaFlag);
                List list = kpiContrastDao.queryHealth(map);
                Map resourceCount = kpiContrastDao.queryResourceCount(kbpCode, areaFlag);
                list.add(resourceCount);
                resultMap.put(kbpCode, list);//用kbpCode做key
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public List queryKpiContrastInfo(String extUnitId, String kpiCode, String timeHorizon) {
        HashMap map = new HashMap();
        if (!"null".equals(extUnitId) && !"".equals(extUnitId)) {
            map.put("extUnitId", extUnitId);
        }
        if (!"null".equals(kpiCode) && !"".equals(kpiCode)) {
            map.put("kpiCode", kpiCode);
        }
        if ("showOneWeek".equals(timeHorizon)) {
            //一周前
            String startTime = alarmAnalysisServiceImpl.sevenDaysBefore();
            String oneTableName = "tb_kpi_value_month_" + startTime.split("-")[0] + startTime.split("-")[1];
            //昨天的日期
            String endTime = alarmAnalysisServiceImpl.yesterdayTime();
            String twoTableName = "tb_kpi_value_month_" + endTime.split("-")[0] + endTime.split("-")[1];
            /*map.put("oneTableName", oneTableName);
            map.put("twoTableName", twoTableName);
            map.put("startTime", startTime);
            map.put("endTime", endTime);*/
            map.put("oneTableName", "tb_kpi_value_month_201801");
            map.put("twoTableName", "tb_kpi_value_month_201801");
            map.put("startTime", "2018-01-01");
            map.put("endTime", "2018-01-07");
        } else if ("showOneMonth".equals(timeHorizon)) {
            //一月前
            String startTime = alarmAnalysisServiceImpl.oneMonthBefore();
            String oneTableName = "tb_kpi_value_month_" + startTime.split("-")[0] + startTime.split("-")[1];
            //昨天的日期
            String endTime = alarmAnalysisServiceImpl.yesterdayTime();
            String twoTableName = "tb_kpi_value_month_" + endTime.split("-")[0] + endTime.split("-")[1];
            map.put("oneTableName", "tb_kpi_value_month_201801");
            map.put("twoTableName", "tb_kpi_value_month_201801");
            map.put("startTime", "2018-01-01");
            map.put("endTime", "2018-01-31");

           /* map.put("oneTableName", oneTableName);
            map.put("twoTableName", twoTableName);
            map.put("startTime", startTime);
            map.put("endTime", endTime);*/
        } else if ("showOneYear".equals(timeHorizon)) {
            //月前
            String startTime = alarmAnalysisServiceImpl.lastMonthBefore();
            String oneTableName = "tb_kpi_value_year_" + startTime.split("-")[0];
            //昨天的日期
            String endTime = alarmAnalysisServiceImpl.lastMonth();
            String twoTableName = "tb_kpi_value_year_" + endTime.split("-")[0];
            map.put("oneTableName", "tb_kpi_value_year_2018");
            map.put("twoTableName", "tb_kpi_value_year_2018");
            map.put("startTime", "2018-01");
            map.put("endTime", "2018-12");
//            map.put("oneTableName", oneTableName);
//            map.put("twoTableName", twoTableName);
//            map.put("startTime", startTime);
//            map.put("endTime", endTime);
        } else {
            String tableName = loadServiceImpl.queryTableName();
            /*map.put("tableName", tableName);*/
            map.put("tableName", "tb_kpi_value_day_20180131");
            return kpiContrastDao.queryYesterdayKpiContrastInfo(map);
        }
        return kpiContrastDao.queryKpiContrastInfo(map);
    }

    @Override
    public List queryKpi(String extUnitId) {
        return kpiContrastDao.queryKpi(extUnitId);
    }
}
